package com.common.service.judicialcheck.impl;

import com.common.base.DictionaryAware;
import com.common.constants.Constants;
import com.common.converter.JudicialTargetTypeSumCheckConverter;
import com.common.entity.department.DepartmentEntity;
import com.common.entity.department.QDepartmentEntity;
import com.common.entity.judicialcheck.JudicialTargetTypeSumCheckEntity;
import com.common.entity.judicialcheck.QJudicialTargetTypeSumCheckEntity;
import com.common.entity.personal.PersonalEntity;
import com.common.global.BizException;
import com.common.global.BizServiceEnum;
import com.common.global.CommonEnum;
import com.common.query.QueryConditionVo;
import com.common.repository.departmentjob.DepartmentRepository;
import com.common.repository.judicialcheck.JudicialTargetTypeSumCheckRepository;
import com.common.repository.judicialpersonal.PersonalRepository;
import com.common.requestvo.judicialcheck.JudicialTargetTypeSumCheckJobExplainReqVo;
import com.common.requestvo.judicialcheck.JudicialTargetTypeSumCheckRankReqVo;
import com.common.requestvo.judicialcheck.JudicialTargetTypeSumCheckReqVo;
import com.common.requestvo.target.TargetItemAssignReqVo;
import com.common.service.judicialcheck.JudicialTargetTypeSumCheckService;
import com.common.vo.judicialcheck.JudicialDepartmentSumScoreCheckVo;
import com.common.vo.judicialcheck.JudicialTargetTypeSumCheckVo;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author johnny
 * @create 2021-05-17 9:58 上午
 **/
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class JudicialTargetTypeSumCheckServiceImpl implements JudicialTargetTypeSumCheckService {


    private final JPAQueryFactory queryFactory;
    private final JudicialTargetTypeSumCheckRepository judicialTargetTypeSumCheckRepository;
    private final PersonalRepository personalRepository;
    private final DepartmentRepository departmentRepository;


    @Override
    public Page<JudicialTargetTypeSumCheckVo> listByCondition(JudicialTargetTypeSumCheckReqVo judicialTargetTypeSumCheckReqVo, Pageable pageable) {

        List<JudicialTargetTypeSumCheckVo> list = new ArrayList<>();
        QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;

        JPAQuery<Tuple> jpaQuery = queryFactory.select(qJudicialTargetTypeSumCheckEntity, QDepartmentEntity.departmentEntity.departmentName)
                .from(qJudicialTargetTypeSumCheckEntity)
                .leftJoin(QDepartmentEntity.departmentEntity).on(QDepartmentEntity.departmentEntity.id.eq(qJudicialTargetTypeSumCheckEntity.departmentId))
                .offset(pageable.getPageNumber() * pageable.getPageSize())
                .limit(pageable.getPageSize());

        fillCondition(jpaQuery, judicialTargetTypeSumCheckReqVo);

        QueryResults<Tuple> queryResults = jpaQuery.fetchResults();

        queryResults.getResults().forEach(tuple -> {
            JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity = tuple.get(0, JudicialTargetTypeSumCheckEntity.class);
            String departmentName = tuple.get(1, String.class);
            JudicialTargetTypeSumCheckVo judicialTargetTypeSumCheckVo = JudicialTargetTypeSumCheckConverter.INSTANCE.domain2vo(judicialTargetTypeSumCheckEntity);
            judicialTargetTypeSumCheckVo.setDepartmentName(departmentName);
            list.add(judicialTargetTypeSumCheckVo);
        });
        return new PageImpl<>(list, pageable, queryResults.getTotal());
    }

    public JudicialTargetTypeSumCheckServiceImpl(JPAQueryFactory queryFactory, JudicialTargetTypeSumCheckRepository judicialTargetTypeSumCheckRepository, PersonalRepository personalRepository, DepartmentRepository departmentRepository) {
        this.queryFactory = queryFactory;
        this.judicialTargetTypeSumCheckRepository = judicialTargetTypeSumCheckRepository;
        this.personalRepository = personalRepository;
        this.departmentRepository = departmentRepository;
    }

    @Override
    public void add(JudicialTargetTypeSumCheckReqVo judicialTargetTypeSumCheckReqVo) {
        judicialTargetTypeSumCheckReqVo.setId(null);
        Long personalId = judicialTargetTypeSumCheckReqVo.getPersonalId();
        if (personalId != null) {
            if (judicialTargetTypeSumCheckRepository.countByPersonalIdAndYearAndMonth(personalId,
                    judicialTargetTypeSumCheckReqVo.getYear(), judicialTargetTypeSumCheckReqVo.getMonth()) > 0) {
                throw new BizException(BizServiceEnum.JUDICIAL_TARGET_TYPE_SUM_CHECK_EXIST);
            }
            PersonalEntity personalEntity = personalRepository.findById(personalId).orElse(null);
            if (personalEntity == null) {
                throw new BizException(BizServiceEnum.FIND_NOT_PERSONAL_INFO);
            }

            JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity = JudicialTargetTypeSumCheckConverter.INSTANCE.vo2domain(judicialTargetTypeSumCheckReqVo);
            judicialTargetTypeSumCheckEntity.setPersonalName(personalEntity.getName());
            judicialTargetTypeSumCheckEntity.setPersonCategoryType(personalEntity.getPersonCategoryType());
            judicialTargetTypeSumCheckEntity.setDepartmentId(personalEntity.getDepartmentId());

            //总分
            calSumScore(judicialTargetTypeSumCheckEntity);
            judicialTargetTypeSumCheckRepository.save(judicialTargetTypeSumCheckEntity);
        } else {
            throw new BizException(BizServiceEnum.FIND_NOT_PERSONAL_INFO);
        }
    }

    private void calSumScore(JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity) {

        BigDecimal init = new BigDecimal(0);
        BigDecimal sumScore = init.add(judicialTargetTypeSumCheckEntity.getEffectScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getEffectScore())
                .add(judicialTargetTypeSumCheckEntity.getEfficiencyScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getEfficiencyScore())
                .add(judicialTargetTypeSumCheckEntity.getQualityScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getQualityScore())
                .add(judicialTargetTypeSumCheckEntity.getJudicialSkillScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getJudicialSkillScore())
                .add(judicialTargetTypeSumCheckEntity.getJudicialStyleScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getJudicialStyleScore())
                .add(judicialTargetTypeSumCheckEntity.getFlowControlScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getFlowControlScore())
                .add(judicialTargetTypeSumCheckEntity.getEfficiencyCheckScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getEfficiencyCheckScore())
                .add(judicialTargetTypeSumCheckEntity.getProfessionalIntegrityScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getProfessionalIntegrityScore())
                .add(judicialTargetTypeSumCheckEntity.getInterpretationScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getInterpretationScore())
                .add(judicialTargetTypeSumCheckEntity.getQualityControlScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getQualityControlScore())
                .add(judicialTargetTypeSumCheckEntity.getEffectCheckScore() == null ? new BigDecimal(0) : judicialTargetTypeSumCheckEntity.getEffectCheckScore());

        log.info("【personalId : {} sumScore: {} 】", judicialTargetTypeSumCheckEntity.getPersonalId(), sumScore.toString());
        judicialTargetTypeSumCheckEntity.setSumScore(sumScore);
    }

    @Override
    public void edit(JudicialTargetTypeSumCheckReqVo judicialTargetTypeSumCheckReqVo) {
        if (judicialTargetTypeSumCheckReqVo.getId() != null && judicialTargetTypeSumCheckReqVo.getId() != 0L) {
            if (judicialTargetTypeSumCheckRepository.existsById(judicialTargetTypeSumCheckReqVo.getId())) {
                JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity = JudicialTargetTypeSumCheckConverter.INSTANCE.vo2domain(judicialTargetTypeSumCheckReqVo);
                PersonalEntity personalEntity = personalRepository.findById(judicialTargetTypeSumCheckReqVo.getPersonalId()).orElse(null);
                if (personalEntity == null) {
                    throw new BizException(BizServiceEnum.FIND_NOT_PERSONAL_INFO);
                }
                judicialTargetTypeSumCheckEntity.setPersonalName(personalEntity.getName());
                judicialTargetTypeSumCheckEntity.setPersonCategoryType(personalEntity.getPersonCategoryType());
                judicialTargetTypeSumCheckEntity.setDepartmentId(personalEntity.getDepartmentId());
                //总分
                calSumScore(judicialTargetTypeSumCheckEntity);
                judicialTargetTypeSumCheckRepository.save(judicialTargetTypeSumCheckEntity);
            } else {
                throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
            }
        } else {
            throw new BizException(CommonEnum.ID_NOT_NULL);
        }
    }

    @Override
    public void delete(Long id) {
        if (judicialTargetTypeSumCheckRepository.existsById(id)) {
            judicialTargetTypeSumCheckRepository.deleteById(id);
        } else {
            throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
        }
    }

    /**
     * 分数图表接口  根据
     *
     * @param judicialTargetTypeSumCheckRankReqVo
     * @return
     */
    @Override
    public List<JudicialTargetTypeSumCheckVo> scoreChartRanking(JudicialTargetTypeSumCheckRankReqVo judicialTargetTypeSumCheckRankReqVo) {
        //填充 Limit
        fillLimit(judicialTargetTypeSumCheckRankReqVo);

        List<JudicialTargetTypeSumCheckVo> list = new ArrayList<>();
        QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;
        JPAQuery<Tuple> jpaQuery = queryFactory.select(
                qJudicialTargetTypeSumCheckEntity.effectScore.sum(),
                qJudicialTargetTypeSumCheckEntity.efficiencyScore.sum(),
                qJudicialTargetTypeSumCheckEntity.qualityScore.sum(),
                qJudicialTargetTypeSumCheckEntity.judicialSkillScore.sum(),
                qJudicialTargetTypeSumCheckEntity.judicialStyleScore.sum(),
                qJudicialTargetTypeSumCheckEntity.professionalIntegrityScore.sum(),
                qJudicialTargetTypeSumCheckEntity.sumScore.sum(),
                qJudicialTargetTypeSumCheckEntity.personalId)
                .from(qJudicialTargetTypeSumCheckEntity)
                //按照 总分数 降序排序
                .groupBy(qJudicialTargetTypeSumCheckEntity.personalId)
                .orderBy(qJudicialTargetTypeSumCheckEntity.sumScore.sum().desc());
        //添加筛选条件
        scoreChartRankingCondition(jpaQuery, judicialTargetTypeSumCheckRankReqVo);

        jpaQuery.fetchResults().getResults().forEach(tuple -> {
            buildSumCheckVoForChartRanking(tuple, list);
        });
        return list;
    }

    /**
     * 构建 个人 人员图表统计的 JudicialTargetTypeSumCheckVo
     *
     * @param tuple
     * @param list
     */
    private void buildSumCheckVoForChartRanking(Tuple tuple, List<JudicialTargetTypeSumCheckVo> list) {

        BigDecimal effectScoreBigDecimal = tuple.get(0, BigDecimal.class);
        BigDecimal efficiencyScoreBigDecimal = tuple.get(1, BigDecimal.class);
        BigDecimal qualityScoreBigDecimal = tuple.get(2, BigDecimal.class);
        BigDecimal judicialSkillScoreBigDecimal = tuple.get(3, BigDecimal.class);

        BigDecimal judicialStyleScoreBigDecimal = tuple.get(4, BigDecimal.class);
        BigDecimal professionalIntegrityScoreBigDecimal = tuple.get(5, BigDecimal.class);
        BigDecimal sumScoreBigDecimal = tuple.get(6, BigDecimal.class);
        Long personalId = tuple.get(7, Long.class);

        JudicialTargetTypeSumCheckVo judicialTargetTypeSumCheckVo = new JudicialTargetTypeSumCheckVo();
        judicialTargetTypeSumCheckVo.setPersonalId(personalId);
        judicialTargetTypeSumCheckVo.setEffectScore(effectScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setEfficiencyScore(efficiencyScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setQualityScore(qualityScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setJudicialSkillScore(judicialSkillScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setJudicialStyleScore(judicialStyleScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setProfessionalIntegrityScore(professionalIntegrityScoreBigDecimal);
        judicialTargetTypeSumCheckVo.setSumScore(sumScoreBigDecimal);

        if (personalId != null) {
            PersonalEntity personalEntity = personalRepository.findById(personalId).orElse(null);
            if (personalEntity != null) {
                judicialTargetTypeSumCheckVo.setPersonalName(personalEntity.getName());
                DepartmentEntity departmentEntity = DictionaryAware.DEPARTMENT_MAP.get(personalEntity.getDepartmentId());
                if (departmentEntity != null) {
                    judicialTargetTypeSumCheckVo.setDepartmentName(departmentEntity.getDepartmentName());
                }
            }
            list.add(judicialTargetTypeSumCheckVo);
        }
    }

    /**
     * 个人图表统计的  condition
     *
     * @param jpaQuery
     * @param judicialTargetTypeSumCheckRankReqVo
     */
    private void scoreChartRankingCondition(JPAQuery<Tuple> jpaQuery, JudicialTargetTypeSumCheckRankReqVo judicialTargetTypeSumCheckRankReqVo) {
        QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;
        if (judicialTargetTypeSumCheckRankReqVo.getYear() != null && judicialTargetTypeSumCheckRankReqVo.getYear() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.year.eq(judicialTargetTypeSumCheckRankReqVo.getYear()));
        }
        if (judicialTargetTypeSumCheckRankReqVo.getMonth() != null && judicialTargetTypeSumCheckRankReqVo.getMonth() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.month.eq(judicialTargetTypeSumCheckRankReqVo.getMonth()));
        }
        if (CollectionUtils.isNotEmpty(judicialTargetTypeSumCheckRankReqVo.getPersonalIdList())) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personalId.in(judicialTargetTypeSumCheckRankReqVo.getPersonalIdList()));
        }
        if (StringUtils.isNotEmpty(judicialTargetTypeSumCheckRankReqVo.getPersonCategoryType())) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personCategoryType.eq(judicialTargetTypeSumCheckRankReqVo.getPersonCategoryType()));
        }
        if (judicialTargetTypeSumCheckRankReqVo.getDepartmentId() != null && judicialTargetTypeSumCheckRankReqVo.getDepartmentId() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.departmentId.eq(judicialTargetTypeSumCheckRankReqVo.getDepartmentId()));
        }
    }

    private void fillLimit(JudicialTargetTypeSumCheckRankReqVo judicialTargetTypeSumCheckRankReqVo) {
        if (judicialTargetTypeSumCheckRankReqVo.getRankLimit() == null || judicialTargetTypeSumCheckRankReqVo.getRankLimit() == 0) {
            Integer limit = DictionaryAware.CHART_RANK_LIMIT.get(Constants.CHART_RANK_LIMIT);
            if (limit != null && limit > 0) {
                //字典配置了 从字典拿
                judicialTargetTypeSumCheckRankReqVo.setRankLimit(limit);
            } else {
                //默认20条
                judicialTargetTypeSumCheckRankReqVo.setRankLimit(Constants.CHART_RANK_DEFAULT_LIMIT);
            }
        }
    }

    private void soreRankFillCondition(JPAQuery<JudicialTargetTypeSumCheckEntity> jpaQuery1, JPAQuery<Tuple> jpaQuery2, JudicialTargetTypeSumCheckRankReqVo judicialTargetTypeSumCheckRankReqVo) {
        JPAQuery jpaQuery = null;
        if (jpaQuery1 != null) {
            jpaQuery = jpaQuery1;
        }
        if (jpaQuery2 != null) {
            jpaQuery = jpaQuery2;
        }
        if (jpaQuery != null) {
            QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;
            if (judicialTargetTypeSumCheckRankReqVo.getYear() != null && judicialTargetTypeSumCheckRankReqVo.getYear() != 0) {
                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.year.eq(judicialTargetTypeSumCheckRankReqVo.getYear()));
            }
            if (judicialTargetTypeSumCheckRankReqVo.getMonth() != null && judicialTargetTypeSumCheckRankReqVo.getMonth() != 0) {
                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.month.eq(judicialTargetTypeSumCheckRankReqVo.getMonth()));
            }
            if (StringUtils.isNotEmpty(judicialTargetTypeSumCheckRankReqVo.getPersonCategoryType())) {
                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personCategoryType.eq(judicialTargetTypeSumCheckRankReqVo.getPersonCategoryType()));
            }
            if (judicialTargetTypeSumCheckRankReqVo.getDepartmentId() != null && judicialTargetTypeSumCheckRankReqVo.getDepartmentId() != 0) {
                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.departmentId.eq(judicialTargetTypeSumCheckRankReqVo.getDepartmentId()));
            }
            if (CollectionUtils.isNotEmpty(judicialTargetTypeSumCheckRankReqVo.getPersonalIdList())) {
                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personalId.in(judicialTargetTypeSumCheckRankReqVo.getPersonalIdList()));
            }
//            if (judicialTargetTypeSumCheckRankReqVo.getPersonalId() != null && judicialTargetTypeSumCheckRankReqVo.getPersonalId() != 0) {
//                jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personalId.eq(judicialTargetTypeSumCheckRankReqVo.getPersonalId()));
//            }
        }

    }

    /**
     * 分数图表接口  部门
     *
     * @param judicialTargetTypeSumCheckRankReqVo
     * @return
     */
    @Override
    public List<JudicialDepartmentSumScoreCheckVo> scoreChartDepartmentRanking(JudicialTargetTypeSumCheckRankReqVo judicialTargetTypeSumCheckRankReqVo) {

        QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;
        QDepartmentEntity qDepartmentEntity = QDepartmentEntity.departmentEntity;

        JPAQuery<Tuple> jpaQuery = queryFactory.select(qJudicialTargetTypeSumCheckEntity.sumScore.sum(),
                qDepartmentEntity.id, qDepartmentEntity.departmentName)
                .from(qJudicialTargetTypeSumCheckEntity)
                .rightJoin(qDepartmentEntity).on(qDepartmentEntity.id.eq(qJudicialTargetTypeSumCheckEntity.departmentId))
                .groupBy(qDepartmentEntity.id)
                .orderBy(qJudicialTargetTypeSumCheckEntity.sumScore.sum().desc());

        soreRankFillCondition(null, jpaQuery, judicialTargetTypeSumCheckRankReqVo);
        //按照 总分数 降序排序

        QueryResults<Tuple> queryResults = jpaQuery.fetchResults();
        List<JudicialDepartmentSumScoreCheckVo> list = new ArrayList<>();
        queryResults.getResults().forEach(tuple -> {
            BigDecimal sumScoreBigDecimal = tuple.get(0, BigDecimal.class);
            Long departmentId = tuple.get(1, Long.class);
            String departmentName = tuple.get(2, String.class);

            JudicialDepartmentSumScoreCheckVo judicialDepartmentSumScoreCheckVo = new JudicialDepartmentSumScoreCheckVo();
            judicialDepartmentSumScoreCheckVo.setDepartmentId(departmentId);
            if (sumScoreBigDecimal == null) {
                sumScoreBigDecimal = new BigDecimal(0);
            }
            judicialDepartmentSumScoreCheckVo.setSumScore(sumScoreBigDecimal);
            judicialDepartmentSumScoreCheckVo.setDepartmentName(departmentName);
            list.add(judicialDepartmentSumScoreCheckVo);
        });

        List<Long> existDepartmentIdList = list.stream().map(JudicialDepartmentSumScoreCheckVo::getDepartmentId).collect(Collectors.toList());

        List<DepartmentEntity> noScoreDepartmentList = departmentRepository.findByIdNotIn(existDepartmentIdList);
        noScoreDepartmentList.forEach(departmentEntity -> {
            JudicialDepartmentSumScoreCheckVo judicialDepartmentSumScoreCheckVo = new JudicialDepartmentSumScoreCheckVo();
            judicialDepartmentSumScoreCheckVo.setDepartmentId(departmentEntity.getId());
            judicialDepartmentSumScoreCheckVo.setDepartmentName(departmentEntity.getDepartmentName());
            judicialDepartmentSumScoreCheckVo.setSumScore(new BigDecimal(0));
            list.add(judicialDepartmentSumScoreCheckVo);
        });
        return list;

    }

    /**
     * @param targetItemAssignReqVo
     */
    @Override
    public void initJudicialTargetTypeSumCheck(TargetItemAssignReqVo targetItemAssignReqVo) {
        //删除老的 类型汇总考核记录
        if (judicialTargetTypeSumCheckRepository.countByPersonalIdAndYearAndMonth(targetItemAssignReqVo.getPersonalId(),
                targetItemAssignReqVo.getYear(), targetItemAssignReqVo.getMonth()) > 0) {
            judicialTargetTypeSumCheckRepository.deleteByPersonalIdAndYearAndMonth(targetItemAssignReqVo.getPersonalId(),
                    targetItemAssignReqVo.getYear(), targetItemAssignReqVo.getMonth());
        }
        PersonalEntity personalEntity = personalRepository.findById(targetItemAssignReqVo.getPersonalId()).orElse(null);
        if (personalEntity != null) {
            JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity = new JudicialTargetTypeSumCheckEntity();
            judicialTargetTypeSumCheckEntity.setPersonalId(targetItemAssignReqVo.getPersonalId());
            judicialTargetTypeSumCheckEntity.setPersonalName(personalEntity.getName());
            judicialTargetTypeSumCheckEntity.setSumScore(new BigDecimal(0));
            judicialTargetTypeSumCheckEntity.setDepartmentId(personalEntity.getDepartmentId());
            judicialTargetTypeSumCheckEntity.setPersonCategoryType(personalEntity.getPersonCategoryType());
            judicialTargetTypeSumCheckEntity.setYear(targetItemAssignReqVo.getYear());
            judicialTargetTypeSumCheckEntity.setMonth(targetItemAssignReqVo.getMonth());
            calSumScore(judicialTargetTypeSumCheckEntity);
            judicialTargetTypeSumCheckRepository.save(judicialTargetTypeSumCheckEntity);
        } else {
            throw new BizException(BizServiceEnum.FIND_NOT_PERSONAL_INFO);
        }

    }

    /**
     * 业务岗位说明书 中的，  个人分值 接口  需要传 personalId  year month 才能定位到具体的一条
     *
     * @param judicialTargetTypeSumCheckJobExplainReqVo
     * @return
     */
    @Override
    public JudicialTargetTypeSumCheckVo findSumCheckDetail(JudicialTargetTypeSumCheckJobExplainReqVo judicialTargetTypeSumCheckJobExplainReqVo) {
        JudicialTargetTypeSumCheckVo judicialTargetTypeSumCheckVo;
        if (judicialTargetTypeSumCheckJobExplainReqVo.getPersonalId() != null && judicialTargetTypeSumCheckJobExplainReqVo.getPersonalId() != 0L) {

            JudicialTargetTypeSumCheckEntity judicialTargetTypeSumCheckEntity = queryFactory.selectFrom(QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity)
                    .where(QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity.personalId.eq(judicialTargetTypeSumCheckJobExplainReqVo.getPersonalId()))
                    .orderBy(QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity.createTime.desc())
                    .limit(1)
                    .fetchOne();
            if (judicialTargetTypeSumCheckEntity != null) {
                judicialTargetTypeSumCheckVo = JudicialTargetTypeSumCheckConverter.INSTANCE.domain2vo(judicialTargetTypeSumCheckEntity);
            } else {
                judicialTargetTypeSumCheckVo = new JudicialTargetTypeSumCheckVo();
            }
        } else {
            throw new BizException(CommonEnum.PARAMS_SHORTAGE);
        }
        return judicialTargetTypeSumCheckVo;
    }

    @Override
    public void fillCondition(JPAQuery<Tuple> jpaQuery, QueryConditionVo queryConditionVo) {

        JudicialTargetTypeSumCheckReqVo judicialTargetTypeSumCheckReqVo = (JudicialTargetTypeSumCheckReqVo) queryConditionVo;
        QJudicialTargetTypeSumCheckEntity qJudicialTargetTypeSumCheckEntity = QJudicialTargetTypeSumCheckEntity.judicialTargetTypeSumCheckEntity;
        if (judicialTargetTypeSumCheckReqVo.getDepartmentId() != null && judicialTargetTypeSumCheckReqVo.getDepartmentId() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.departmentId.eq(judicialTargetTypeSumCheckReqVo.getDepartmentId()));
        }
        if (judicialTargetTypeSumCheckReqVo.getYear() != null && judicialTargetTypeSumCheckReqVo.getYear() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.year.eq(judicialTargetTypeSumCheckReqVo.getYear()));
        }
        if (judicialTargetTypeSumCheckReqVo.getMonth() != null && judicialTargetTypeSumCheckReqVo.getMonth() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.month.eq(judicialTargetTypeSumCheckReqVo.getMonth()));
        }
        if (judicialTargetTypeSumCheckReqVo.getPersonalId() != null && judicialTargetTypeSumCheckReqVo.getPersonalId() != 0) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personalId.eq(judicialTargetTypeSumCheckReqVo.getPersonalId()));
        }
        if (StringUtils.isNotEmpty(judicialTargetTypeSumCheckReqVo.getPersonalName())) {
            jpaQuery.where(qJudicialTargetTypeSumCheckEntity.personalName.like("%" + judicialTargetTypeSumCheckReqVo.getPersonalId() + "%"));
        }
    }
}